import {
    ElMessageBox
} from 'element-plus' ;

export default class{

    #title ;

    #message ;

    #type ;

    #applyActions ;

    #cancelActions ;

    constructor({
        title,
        message,
        type = 'info',
        apply,
        cancel
    } , {
        context,
        ActionProcessor
    }){

        const me = this ;

        me.#title = title ;

        me.#message = message ;

        me.#type = type ;

        if(apply){

            me.#applyActions = new ActionProcessor(apply , context);
        }

        if(cancel){

            me.#cancelActions = new ActionProcessor(cancel , context) ;
        }
    }

    execute(params , vars){

        const 
            me = this,
            applyActions = me.#applyActions,
            cancelActions = me.#cancelActions;

        ElMessageBox.prompt(
            me.#message,
            me.#title,
            {
                type:me.#type
            }
        )
            .then(({
                value
            }) => {
               
                if(applyActions){

                    params.params = [
                        value
                    ] ;

                    applyActions.execute(params , {
                        ...vars
                    }) ;
                }

            })
            .catch(() => {
                
                if(cancelActions){

                    cancelActions.execute(params , {
                        ...vars
                    }) ;
                }

            }) ;

        return {
            next:false
        }
    }
}